// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var router = require("../hash/pageRouter");
	var footer = require("../footer/footer_main");
	var dialog = require("dialog");
	var css_receivableNoticeList = require("./receivableNoticeList.css");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var financeId = "";
	var init = function() {
		footer.init();
		$("#footer").removeClass("none");
		leftMenu.init(5);
		topMenu.init(1);
		$(".footer").removeClass("none");
		$(".table-section").removeClass("none");
		$("#leftMenu6").find(".leftMenu-item").removeClass("leftMenu-active");
		$("#leftMenu6 .leftMenu li:first-child").addClass("leftMenu-open");
		$(".receivableNoticeList").addClass("leftMenu-active");
		inquireReceivablesPageList();

		initEvent();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		//放款单查询
		$(".loanNoticeList").unbind("click").click(function(){
		location.href="../financing/loanNoticeList.html";
		});
		//放货查询
		$(".deliveryNoticeList").unbind("click").click(function(){
		location.href="../financing/deliveryNoticeList.html";
		});
		//收款单查询
		$(".receivableNoticeList").unbind("click").click(function(){
		location.href="../financing/receivableNoticeList.html";
		});
		//已完成融资
		$(".endFinanceList").unbind("click").click(function(){
		location.href="../financing/endFinanceList.html";
		});
		//收货通知查询
		$(".recieveNoticeList").unbind("click").click(function(){
		location.href="../financing/recieveNoticeList.html";
		});
		//左侧菜单栏	解除监管
		$(".cancelSuperviseA").unbind("click").click(function() {
			router.route("main/supervisionRelieveList.html?left=9", true);
		});

		//左侧菜单栏	待审批
		$(".financeUnexamA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=0&financeState=1", true);
		});

		//左侧菜单栏	合同上传
		$(".contractUnsignA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=1&financeState=3", true);
		});

		//左侧菜单栏	收货通知
		$(".noticeReceiveA").unbind("click").click(function() {
			router.route("main/main_finance.html?left=2&financeState=5", true);
		});

		//左侧菜单栏 放款确认
		$(".financeUnreleaseA").unbind("click").click(function() {
			router.route("main/loanNotice_list.html?left=4", true);
		});

		//左侧菜单栏  放货通知
		$(".financeUnreceiveBulkstockA").unbind("click").click(function() {
			router.route("main/deliveryNotice_list.html?left=8", true);
		});

		//左侧菜单栏 收款确认
		$(".financeUnreceiveA").unbind("click").click(function() {
			router.route("main/receivablesNotice_list.html?left=7", true);
		});

		//左侧菜单栏 通知运输
		$(".financeUntransportA").unbind("click").click(function() {
			router.route("main/transportNotice_list.html?left=5", true);
		});


		//左侧菜单栏 提货审批
		$(".financePickupExamA").unbind("click").click(function() {
			router.route("main/pickupList.html?left=6", true);
		});

		//左侧菜单栏 监管确认
		$(".financeUnsuperviseA").unbind("click").click(function() {
			router.route("main/supervisionNotice_list.html?left=3", true);
		});

		//左侧菜单栏 延期计息
		$(".interestDelayA").unbind("click").click(function() {
			router.route("main/interestDelayList.html?left=12", true);
		});

		//左侧菜单栏 延期还款
		$(".pickupDelayA").unbind("click").click(function() {
			router.route("main/pickupDelayList.html?left=11", true);
		});

		//左侧菜单栏 提前还款
		$(".pickupPreA").unbind("click").click(function() {
			router.route("main/pickupPreList.html?left=10", true);
		});

		//左侧菜单栏 赎货申请
		$(".pickupRansomA").unbind("click").click(function() {
			router.route("main/pickupRansomList.html?left=14", true);
		});

		//左侧菜单栏 扣货申请
		$(".detainedA").unbind("click").click(function() {
			router.route("main/detainedList.html?left=13", true);
		});

		//左侧菜单栏 扣货申请
		$(".badassetsHandleA").unbind("click").click(function() {
			router.route("main/badassetsHandleList.html?left=15", true);
		});

		//左侧菜单栏 异常管理
		$(".abnormalManageA").unbind("click").click(function() {
			//			router.route("exception/exceptionManage.html?left=16", true);
			location.href = "../exception/exceptionManage.html";
		});
		
		//查询
		
		$("#receivableNoticeSearch").unbind("click").click(function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireReceivablesPageList();
		});
	};
	
	var inquireReceivablesPageList = function(){
		
		var beginDate = $("#beginDate").val();
		var endDate = $("#endDate").val();
		var financeNumber = $("#financeNumber").val();
		var enterpriseName = $("#enterpriseName").val();
		base.fnAjax({
			action: "inquireReceivablesPageList.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				beginDate: beginDate,
				endDate: endDate,
				financeNumber: financeNumber,
				enterpriseName: enterpriseName
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#receivableNoticeListTpl").html();
					var html = juicer(tpl, data);
					$("#receivableNoticeListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#pagenation"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireReceivablesPageList();
						});
						
						$(".detail").unbind("click").click(function(){
							var receivablesId =$(this).parent().parent().attr("receivablesId");
							router.route("financing/receivableNoticeDetail.html?receivablesId="+receivablesId,true);
						});
						$(".approve").unbind("click").click(function(){
							var receivablesId =$(this).parent().parent().attr("receivablesId");
							router.route("financing/receivableNoticeDetail.html?receivablesId="+receivablesId+"&type=approve",true);
//							dialog.showConfirmDialog("收款确认","是否要确认此收款记录？",function(){
//								confirmReceivableNotice(receivablesId);
//							});
						});
						
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	}
	var confirmReceivableNotice = function(receivablesId){
		
		base.fnAjax({
			action: "confirmReceivableNotice.json",
			oData: {
				receivablesId:receivablesId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "收款单确认成功");
					inquireReceivablesPageList();
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	}
	return {
		init: init
	};
});